﻿using System;
using System.Json;
using System.Linq;
using ToodledoAPI.DataTypes;
using ToodledoAPI.Exceptions;

namespace ToodledoAPI
{
    public static class locations
    {
        public static void get(Key key, Action<Location[]> returnAction)
        {
            string Method = String.Format("locations/get.php?key={0}", key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new locationException(s);
                }
                JsonArray Array = (JsonArray)JsonObject.Parse(s);
                returnAction(Array.Select((F) => new Location((JsonObject)F)).ToArray());
            });
        }

        public static void add(Key key, int id, Action<Location> returnAction,
            string name, string description, decimal? lat, decimal? lon)
        {
            string Method = String.Format("locations/add.php?key={0};id={1};name={2}", key, id, name);
            if (description != null)
                Method += String.Format(";description={0}", description);
            if (lat != null)
                Method += String.Format(";lat={0}", lat);
            if (lon != null)
                Method += String.Format(";lon={0}", lat);
            ApplicationConfiguration.CallWebService(Method, (s) => returnAction(new Location(s)));
        }

        public static void edit(Key key, int id, Action<Location> returnAction,
            string name, string description, decimal? lat, decimal? lon)
        {
            string Method = String.Format("locations/edit.php?key={0};id={1}", key, id);
            if (Helpers.AllNull(name, description, lat, lon))
                return;
            if (name != null)
                Method += String.Format(";name={0}", name);
            if (description != null)
                Method += String.Format(";description={0}", description);
            if (lat != null)
                Method += String.Format(";lat={0}", lat);
            if (lon != null)
                Method += String.Format(";lon={0}", lat);
            ApplicationConfiguration.CallWebService(Method, (s) => returnAction(new Location(s)));
        }

        public static void delete(int id, Key key, Action<int> returnAction_Success)
        {
            string Method = String.Format("locations/delete.php?id={0};key={1}", id, key);
            ApplicationConfiguration.CallWebService(Method, (s) =>
            {
                if (s.IsToodledoException())
                {
                    throw new locationException(s);
                }
                returnAction_Success(int.Parse(((JsonObject)JsonObject.Parse(s))["deleted"]));
            });
        }
    }
    namespace DataTypes
    {
        public class Location
        {
            public Location(string s)
            {
                if (s.IsToodledoException())
                    throw new locationException(s);
                JsonArray Array = (JsonArray)JsonObject.Parse(s);
                Load((JsonObject)Array[0]);
            }
            public Location(JsonObject G)
            {
                Load(G);
            }
            void Load(JsonObject G)
            {
                id = int.Parse(G["id"]);
                name = G["name"];
                description = G["description"];
                lat = decimal.Parse(G["lat"]);
                lon = decimal.Parse(G["lon"]);
            }

            public int id { get; set; }
            /// <summary>
            /// A text string up to 255 characters
            /// </summary>
            public string name { get; set; }
            /// <summary>
            /// A text string up to 255 characters used to describe the locaiton 
            /// (address, phone number, hours of operation, etc).
            /// </summary>
            public string description { get; set; }
            /// <summary>
            /// A decimal representing the latitude
            /// </summary>
            public decimal lat { get; set; }
            /// <summary>
            /// A decimal representing the longitude
            /// </summary>
            public decimal lon { get; set; }
        }
    }
}
